package com.tanhua.backstage.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.backstage.vo.HtMessageVideoVo;
import com.tanhua.backstage.vo.PageResult;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.CommentType;
import com.tanhua.dubbo.server.vo.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class VideoService {
    @Reference(version = "1.0.0")
    private VideoApi videoApi;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    /**
     * 业务层实现功能:获取视频翻页的信息
     *
     * @param page
     * @param pageSize
     * @param sortProp
     * @param sortOrder
     * @param uid
     * @return
     */
    public PageResult getVideoInfo(Integer page, Integer pageSize, String sortProp, String sortOrder, Long uid) {
        try {
            //创建结果集对象
            PageResult pageResult = new PageResult();
            //封装属性
            pageResult.setPage(page);
            pageResult.setPagesize(pageSize);
            //获取视频信息
            PageInfo<Video> pageInfo = videoApi.findAllVideoBackstage(page, pageSize, sortProp, sortOrder, uid);
            //设置查询用户信息的条件
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", uid);
            //获取用户信息
            UserInfo userInfo = userInfoMapper.selectOne(wrapper);
            //封装结果对象
            int count = 1;
            //创建结果集合
            List<HtMessageVideoVo> htMessageVos = new ArrayList<>();
            for (Video video : pageInfo.getRecords()) {
                //封装属性
                HtMessageVideoVo htMessageVo = new HtMessageVideoVo();
                htMessageVo.setId(count++);
                htMessageVo.setNickname(userInfo.getNickName());
                htMessageVo.setUserId(userInfo.getUserId());
                htMessageVo.setCreateDate(video.getCreated());
                htMessageVo.setVideoUrl(video.getVideoUrl());
                htMessageVo.setPicUrl(video.getPicUrl());
                //设置举报数
                htMessageVo.setReportCount(quanZiApi.likeAndLoveCount(null, video.getId().toHexString(), CommentType.REPORT));//举报数
                //设置点赞数
                htMessageVo.setLikeCount(quanZiApi.likeAndLoveCount(null, video.getId().toHexString(), CommentType.LIKE));//点赞数
                //设置评论数
                htMessageVo.setCommentCount(quanZiApi.likeAndLoveCount(null, video.getId().toHexString(), CommentType.COMMENT));//评论数
                //设置转发数
                htMessageVo.setForwardingCount(quanZiApi.likeAndLoveCount(null, video.getId().toHexString(), CommentType.TRANSMIT));//转发数
                //添加到集合中
                htMessageVos.add(htMessageVo);
            }
            //设置总条数
            pageResult.setCounts(pageInfo.getTotal());
            if (pageInfo.getTotal() % pageSize == 0) {
                pageResult.setPages(pageInfo.getTotal() / pageSize);
            } else {
                pageResult.setPages(pageInfo.getTotal() / pageSize + 1);
            }
            //将结果集合封装到pageResult对象中
            pageResult.setItems(htMessageVos);
            return pageResult;
        } catch (Exception e) {
            return null;
        }
    }
}
